using UnityEngine;
using System.Collections;

public class GameHeroItem : BaseItem {
	private Hero mHero;
	
//	public Item.ITEM_TYPE itemType;
	public int itemCount = 0;
	public GameController gameController;
	
	public Item item;
	public UILabel count;
	public GameObject playerSprite;
	// Use this for initialization
	void Start () {
		item.SetItemType(Item.ITEM_TYPE.NULL);
		Refresh();
	}
	
	// Update is called once per frame
	void Update () {
		
	}
	public void OnDrop(GameObject go){
		Item itemTmp = go.GetComponent<Item>();
		if(itemTmp.IsNull() || !itemTmp.IsCanDrag() || itemTmp.isPlayer || itemCount >= GameController.Player_Max_Count){
			return ;
		}
		if(item.IsNull() || item.GetItemType() == itemTmp.GetItemType()){
			item.SetItemType(itemTmp.GetItemType());
			itemCount++;
			StartCoroutine(ItemHoldDown(gameController.itemList[itemTmp.GetRow(),itemTmp.GetColumn()]));
			Transform tran = gameController.itemList[itemTmp.GetRow(),itemTmp.GetColumn()].transform;
			playerSprite.transform.position = new Vector3(tran.position.x,playerSprite.transform.position.y,playerSprite.transform.position.z);
//			yield return StartCoroutine(gameController.StartClearUp());
		}
	}
	public IEnumerator ItemHoldDown(Item itemTmp){
		yield return StartCoroutine(itemTmp.HoldDown());
		Refresh();
	}
	
	public void Clear(){
		item.SetItemType(Item.ITEM_TYPE.NULL);
		itemCount = 0;
		Refresh();
	}

	#region implemented abstract members of BaseItem
	public override void SetItem (object obj)
	{
		mHero = (Hero)obj;
	}

	public override object GetItem ()
	{
		return mHero;
	}

	public override void Refresh(){
		if(!item.IsNull()){
			NGUITools.SetActive(item.gameObject,true);
			NGUITools.SetActive(count.gameObject,true);
//			icon.spriteName = ((int)itemType).ToString();
			count.text = "x "+ itemCount;
		}else{
			NGUITools.SetActive(item.gameObject,false);
			NGUITools.SetActive(count.gameObject,false);
		}
	}
	#endregion
}
